// water.frag
#ifdef GL_ES
#define PRECISION mediump
precision mediump float;
#else
#define PRECISION
#endif

uniform sampler2D u_texture;
uniform PRECISION float u_time;
uniform vec2 u_resolution;
uniform float u_waveIntensity;
uniform float u_waveSpeed;
uniform float u_refractionStrength;
uniform vec4 u_waterColor;

varying PRECISION vec4 v_color;
varying PRECISION vec2 v_texCoords;
varying PRECISION vec2 v_worldPos;

void main() {
    // 创建多频率的波纹
    PRECISION vec2 uv = v_texCoords;
    
    // 主波纹
    PRECISION float wave1 = sin(uv.x * 15.0 + u_time * u_waveSpeed) * u_waveIntensity;
    PRECISION float wave2 = cos(uv.y * 12.0 + u_time * u_waveSpeed * 0.7) * u_waveIntensity * 0.8;
    PRECISION float wave3 = sin(uv.x * 8.0 + uv.y * 6.0 + u_time * u_waveSpeed * 1.3) * u_waveIntensity * 0.6;
    
    // 组合波纹效果
    PRECISION vec2 distortion = vec2(wave1 + wave3, wave2 + wave3) * u_refractionStrength;
    
    // 应用折射扭曲
    PRECISION vec2 distortedCoords = uv + distortion;
    
    // 采样纹理（模拟水下折射效果）
    PRECISION vec4 baseColor = texture2D(u_texture, distortedCoords);
    
    // 添加水色
    PRECISION vec4 waterTint = u_waterColor;
    PRECISION vec4 finalColor = mix(baseColor, waterTint, waterTint.a);
    
    // 添加波纹高光
    PRECISION float specular = pow(wave1 * wave2 * 2.0 + 0.5, 2.0) * 0.3;
    finalColor.rgb += specular;
    
    gl_FragColor = finalColor * v_color;
}